package com.leunpha.mini.spider.listener;

import com.leunpha.mini.net.events.NetEvent;
import com.leunpha.mini.net.events.NetState;
import com.leunpha.mini.utils.IOUtils;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Observable;

/**
 * User: zhoujingjie
 * Date: 14-5-28
 * Time: 上午11:45
 */
public class DefaultSpiderListener extends SpiderListener {
    protected ObjectOutputStream objectOutputStream;
    protected void prepare(){
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:\\test\\"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    protected void success(Object object){
        try {
            objectOutputStream.writeObject(object);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    protected void end(){
        IOUtils.close(objectOutputStream);
    }
    @Override
    public void update(Observable observable, NetEvent event) {
        if(NetState.BEGIN.equals(event.getValue())){
            prepare();
        }else if(event.getState().equals(NetState.SUCCESS)){
              success(event.getValue());
        }else if(NetState.END.equals(event.getState())){
               end();
        }
    }
}
